package com.example.administrator.startproject_407xjy.model;

import android.net.sip.SipAudioCall;

import com.example.administrator.startproject_407xjy.bean.WeatherResult;
import com.example.administrator.startproject_407xjy.iface.WeatherListener;
import com.example.administrator.startproject_407xjy.iface.Weatheriface;
import com.example.administrator.startproject_407xjy.service.WeatherService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/3/14.
 */

public class Weathermodel implements Weatheriface{
    private Retrofit retrofit;
    private String BASEURL="http://www.weather.com.cn/";
    public Weathermodel() {
        //Retrofit实例化 3-1
        System.out.println("----weathermodel构造函数");
        retrofit=new Retrofit.Builder().baseUrl(BASEURL)//主机地址
        .addConverterFactory(GsonConverterFactory.create())//解析
        .build();
    }

    @Override
    public void getWeather(String cityno, final WeatherListener listener) {
        //Retrofit接口使用 3-2
        WeatherService weatherService=retrofit.create(WeatherService.class);
        Call<WeatherResult> call=weatherService.getResult(cityno);
        // //Retrofit接口使用 3-3
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if (response.isSuccessful()&&response.body()!=null)
                listener.onResponse(response.body().getWeatherinfo());
                else
                    listener.onFail("onResponse error");
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                System.out.println("----weathermodel  onfailure 22222222");
                listener.onFail(t.toString());

            }
        });

    }
}
